package com.cc.police.service.Impl;


import com.alibaba.fastjson.JSON;
import com.cc.police.entity.PUser;
import com.cc.police.mapper.PUserMapper;
import com.cc.police.service.PUserService;
import com.cc.police.service.ex.PasswordNotMatchException;
import com.cc.police.service.ex.UserNotFoundException;
import com.cc.police.util.RedisUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import redis.clients.jedis.Jedis;

import javax.security.auth.login.LoginException;

@Service
public class PUserServiceImpl implements PUserService {
    @Autowired
    PUserMapper mapper;
    @Autowired
    RedisUtil redisUtil;


    private String getMd5Password(String password) {
        // 规则：对password+salt进行3重加密
        String str = password;
        for (int i = 0; i < 3; i++) {
            str = DigestUtils
                    .md5DigestAsHex(str.getBytes()).toUpperCase();
        }
        return str;
    }

//
//    //生成盐值
//    private String generateSalt() {
//        return String.valueOf(System.currentTimeMillis());
//    }


    @Override
    public PUser login(PUser pUser)throws UserNotFoundException, PasswordNotMatchException {

        PUser result = mapper.findByUsername(pUser.getUsername());
//        System.out.println("aaaaa="+result.getUserId());
        // 判断查询结果是否为null
        if (result == null) {
            throw new UserNotFoundException(
                    "登录失败！用户名不存在！");
        }
//        String password = getMd5Password(pUser.getPassword());
        String salt = result.getSalt();
        String encryptPassword = new Md5Hash(pUser.getPassword(), salt).toString();
        if (!result.getPassword().equals(encryptPassword)) {
            throw new PasswordNotMatchException(
                    "登录失败！密码错误！");
        }
        // 密码错误将查询结果中的password设置为null
//        result.setPassword(null);
        return result;


    }

    @Override
    public void addUserToken(String token, Integer userId) {
        Jedis jedis = redisUtil.getJedis();

        jedis.setex("user:" + userId + ":token", 60 * 60 * 24*30, token);//一个月时间

        jedis.close();
    }


    /*设置页面 修改用户资料*/
    @Override
    public void updatePhotoAndName(String avatar, String username,Integer userId) {
        System.err.println("Impl头像="+avatar);
        System.err.println("Impl名字="+username);
        System.err.println("Impl用户id="+userId);
        mapper.updatePhotoAndName(avatar,username,userId);
    }

    /*用户密码修改*/
    @Override
    public void updetePassword(Object userId, String password, String newpassword) {
        PUser result = mapper.findByUserId(userId);
//        String md5Password = getMd5Password(password).toString();
        String salt = result.getSalt();
        String encryptPassword = new Md5Hash(password, salt).toString();

        if(!result.getPassword().equals(encryptPassword)){
            throw new PasswordNotMatchException(
                    "修改密码失败！原密码错误！");
        }

        String encryptPassword1 = new Md5Hash(newpassword, salt).toString();
        mapper.updatePasswordByUserId(userId,encryptPassword1);

    }


    @Override
    public PUser selectAllById(Object userId) {
        return mapper.findByUserId(userId);
    }

    @Override
    public void updateUsername(String username,Integer userId) {
        mapper.updateUsername(username,userId);
    }


}
